<?php

namespace app\model;

use think\Model;

class Storagelocation extends Model
{

    protected $connection = 'mysql';

    protected $pk = 'id';

    protected $name = 'storagelocation';

    public static function getpagearray($sid)
    {
        $data = self::where(['weid' => weid(), 'sid' => $sid, 'status' => 1])
            ->order('sort asc,id asc')
            ->select()->toArray();

        foreach ($data as $k => $v) {
            if ((int) $v['nowquantity'] < (int) $v['quantity']) {
                $array[$k]['val'] = $v['id'];
                $array[$k]['key'] = $v['title'];
            } else {
                $nowquantity = OrderGoods::where(['storagelocation' => $v['id'], 'is_complete' => 0])->count();
                if ((int) $nowquantity < (int) $v['quantity']) {
                    Storagelocation::where('id', $v['id'])->update(['nowquantity' => $nowquantity]);
                    $array[$k]['val'] = $v['id'];
                    $array[$k]['key'] = $v['title'];
                }
            }
        }
        return $array;
    }
}
